package lmycc.test.demo.controller;

import lmycc.test.demo.domain.Menu;
import lmycc.test.demo.service.MenuService;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Component
@Transactional
public class UploadController {
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    Map<String, String> message = new HashMap<String, String>();
    @Autowired
    private MenuService menuService;

    @RequestMapping("/uploadFile")
    @ResponseBody
    public Map<String, String> uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) {
        String fileName = file.getOriginalFilename(); // 获取文件的名称
        String filePath = "F:/Pic1/" + fileName;   // 这是文件的保存路径，如果不设置就会保存到项目的根目录
        System.out.println(filePath);
        File dest = new File(filePath);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            //BufferedOutputStream outputStream = null;
            //outputStream = new BufferedOutputStream(new FileOutputStream(filePath));
            //outputStream.write(file.getBytes());
            //outputStream.flush();
            //outputStream.close();
            file.transferTo(dest);
            message.put("message", "客户资料上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            message.put("message", "有异常！！！");
        }

        return message;
    }

    /*
     * 导出数据 Excel
     * */
    @ResponseBody
    @RequestMapping("/getExcel")
    public String getExcel(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("菜单明细表");
        List<Menu> getExcel = menuService.findAll();
        String fileName = "excel" + ".xls";                                                                             //设置要导出的文件的名字
        int rowNum = 1;                                                                                                 //新增数据行，并且设置单元格数据
        String[] headers = {"编号", "菜单名称", "菜单路径"};
        HSSFRow row = sheet.createRow(0);                                                                       //headers表示excel表中第一行的表头
        for (int i = 0; i < headers.length; i++) {                                                                      //在excel表中添加表头
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        for (Menu menu : getExcel) {                                                                                    //在表中存放查询到的数据放入对应的列
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(menu.getId());
            row1.createCell(1).setCellValue(menu.getName());
            row1.createCell(2).setCellValue(menu.getPath());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        return "success";
    }
}